package Controler;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import Model.Dao.DAODanhMucSanPham;
import Model.Pojo.Danhmucsanpham;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author THANH QUAN
 */
public class ThemDanhMuc
        extends HttpServlet
{

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("QUAN_LY_DANH_MUC", 1);
        try
        {
            Danhmucsanpham dmsp = new Danhmucsanpham();
            dmsp.setTendanhmuc(request.getParameter("txtTenDanhMuc").trim());
            dmsp.setKyhieu(request.getParameter("txtKyHieu").trim());
            dmsp.setSoluongsp(0);
            dmsp.setTinhtrang(Boolean.TRUE);
            DAODanhMucSanPham DAOdmsp = new DAODanhMucSanPham();
            if (!DAOdmsp.KiemTraDanhMucTonTai(dmsp))
            {
                DAOdmsp.ThemDanhMuc(dmsp);
                request.setAttribute("Message", "Thêm danh mục thành công");
            }
            else
            {
                request.setAttribute("Message", "Lỗi: Danh mục đã tồn tại.");
            }
        }
        catch (Exception ex)
        {
            request.setAttribute("Message", ex.getMessage());
        }
        RequestDispatcher view = request.getRequestDispatcher("them-danh-muc.jsp");
        view.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
              if(!CXuLy.PhanQuyen(request, response))
        {
            return;
        }

        request.setAttribute("QUAN_LY_DANH_MUC", 1);
        RequestDispatcher view = request.getRequestDispatcher("them-danh-muc.jsp");
        view.forward(request, response);

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
